package com.fwtai.service.api;

import com.fwtai.api.ApiFollowDao;
import com.fwtai.bean.PageFormData;
import com.fwtai.config.LocalUserId;
import com.fwtai.tool.IdBuilder;
import com.fwtai.tool.ToolClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * 关注|粉丝
 * @作者 田应平
 * @版本 v1.0
 * @QQ号码 444141300
 * @创建日期 2021-07-20 10:40:21
 * @官网 <url>http://www.fwtai.com</url>
*/
@Service
public class ApiFollowService{

    @Resource
    private ApiFollowDao apiFollowDao;

    public String addFollow(final String followId){
        if(followId == null || followId.length() <=0){
            return ToolClient.jsonValidateField();
        }
        final String query = apiFollowDao.queryExist(followId);
        if(query == null){
            return ToolClient.createJsonFail("关注的用户不存在");
        }
        final String loginId = LocalUserId.get();
        if(loginId.equals(followId)){
            return ToolClient.createJsonFail("您不能关注你自己");
        }
        final HashMap<String,String> params = new HashMap<>(3);
        params.put("loginId",loginId);
        params.put("followId",followId);
        final String exist = apiFollowDao.queryFollow(params);
        if(exist == null){
            params.put("kid",String.valueOf(new IdBuilder().getId121L()));
            return ToolClient.executeRows(apiFollowDao.addFollow(params),"关注成功","关注失败");
        }else{
            return ToolClient.executeRows(apiFollowDao.delFollow(params),"取消成功","取消失败");
        }
    }

    //获取我关注的用户
    public String getFollow(final HttpServletRequest request){
        final String loginId = LocalUserId.get();
        final PageFormData formData = ToolClient.pageParamsApi(request);
        if(formData == null) return ToolClient.jsonValidateField();
        formData.put("loginId",loginId);
        return ToolClient.jsonPage(apiFollowDao.getFollow(formData));
    }

    public String delById(final String kid){
        if(kid == null || kid.length() <= 0){
            return ToolClient.jsonValidateField();
        }
        final HashMap<String,String> params = new HashMap<>(2);
        final String loginId = LocalUserId.get();
        params.put("kid",kid);
        params.put("loginId",loginId);
        return ToolClient.executeRows(apiFollowDao.delById(params));
    }
}